package action.newelifile;

import java.util.ArrayList;
import java.util.Locale;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import action.adapter.CopyFTPPopupAdapter;
import action.pojo.FtpConnect;
import action.util.LoadSetting;
import action.util.Tool;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class CopyFTPPopup extends Activity{
	TextView textView,txtAddressFTP,txtViewEmpty;
	ListView listFtpDetailPopup;
	ImageView imgBackButton;
//	FtpConnect ftpConnect;
	ArrayList<FtpConnect> list_ftp_popup;
	FTPFile[] listfileFTP;
	CopyFTPPopupAdapter ftpPopupAdapter;
	Button pasteftp, cancelftp;
	FTPClient gclient=new FTPClient();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initLocale();
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.copyftp_popup);
		getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		initAttribute();
//		ftpConnect = (FtpConnect)getIntent().getExtras().get("ftpconnect");
		textView.setText(Tool.dest_connect_popup.getHostname());
		list_ftp_popup = new ArrayList<FtpConnect>();
		initData();
		pasteftp.setOnClickListener(processPaste());
//		cancelftp.setOnClickListener(processCancel());
	}
	
	private OnClickListener processPaste(){
		OnClickListener onclick = new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
			}
		};
		return onclick;
	}
	
//	private OnClickListener processCancel(){
//		OnClickListener onclick = new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Tool.generalCloseconnect(gclient);
//				Tool.dest_connect_popup = new FtpConnect();
//				Tool.dest_ftp_path_popup.clear();
//				Intent intent = new Intent(CopyFTPPopup.this,FtpActivity.class);
//				startActivity(intent);
//			}
//		};
//		return onclick;
//	}

	public void initAttribute(){
		textView = (TextView)findViewById(R.id.tvHeaderFtp);
		txtAddressFTP = (TextView)findViewById(R.id.txtAddressFTP);
		txtViewEmpty = (TextView)findViewById(R.id.txtViewEmpty);
		listFtpDetailPopup = (ListView)findViewById(R.id.listFtpDetailPopup);
		pasteftp = (Button)findViewById(R.id.btPasteFtp);
		cancelftp = (Button)findViewById(R.id.btCancelFtp);
		imgBackButton = (ImageView)findViewById(R.id.imgBackButton);
		
		pasteftp.setText(getString(R.string.paste));
		cancelftp.setText(getString(R.string.cancel));
		
		cancelftp.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Tool.generalCloseconnect(gclient);
				Tool.dest_connect_popup = new FtpConnect();
				Tool.dest_ftp_path_popup.clear();
				setResult(0);
				finish();
			}
		});
		
		imgBackButton.setOnClickListener(imiBackClick());
	}
	
	public void initLocale(){
		String languageToLoad = Tool.locale(LoadSetting.setting.getLanguage());  
		Locale locale = new Locale(languageToLoad);   
		Locale.setDefault(locale);  
		Configuration config = new Configuration();  
		config.locale = locale;  
		getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());  
	}
	
	public void initData(){
		if(Tool.dest_ftp_path_popup!=null && Tool.dest_ftp_path_popup.size()>0){
			Tool.generalConnects(gclient, Tool.dest_connect_popup);
			refreshFTPServerDetailPopup(Tool.dest_connect_popup);
		}
	}
	
	public void refreshFTPServerDetailPopup(FtpConnect ftpConnect){
		list_ftp_popup = getListFTP(ftpConnect);
		
		if(list_ftp_popup!=null && list_ftp_popup.size()>0){
			listFtpDetailPopup.setVisibility(ListView.VISIBLE);
			txtViewEmpty.setVisibility(TextView.GONE);

			ftpPopupAdapter = new CopyFTPPopupAdapter(this, R.layout.list_view, list_ftp_popup);
			listFtpDetailPopup.setAdapter(ftpPopupAdapter);
			listFtpDetailPopup.setOnItemClickListener(onclickFileItem());
			listFtpDetailPopup.setOnItemLongClickListener(null);
		}else{
			System.out.println(" vao day khong the");
			listFtpDetailPopup.setVisibility(ListView.GONE);
			txtViewEmpty.setVisibility(TextView.VISIBLE);
			txtViewEmpty.setText("Folder empty");
		}
		
		txtAddressFTP.setText(Tool.connect.getHostname()+Tool.getFTPPath(Tool.dest_ftp_path_popup));
	}
	
	private OnClickListener imiBackClick(){
		OnClickListener onclick = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(Tool.dest_ftp_path_popup.size()>=2){
					Tool.dest_ftp_path_popup.remove(Tool.dest_ftp_path_popup.size()-1);
					refreshFTPServerDetailPopup(Tool.dest_connect_popup);
				}
				
			}
		};
		return onclick;
	}
	
	public OnItemClickListener onclickFileItem(){
		OnItemClickListener clickListener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int postion,
					long arg3) {
				Tool.dest_connect_popup = (FtpConnect)arg0.getItemAtPosition(postion);
				Tool.dest_ftp_path_popup.add(Tool.dest_connect_popup.getFtpFile().getName());
				if(Tool.connect.getFtpFile().isDirectory()){
					refreshFTPServerDetailPopup(Tool.dest_connect_popup);
				}else{
					listFtpDetailPopup.setOnItemClickListener(null);
				}
			}
		};
		return clickListener;
	}
	
	public ArrayList<FtpConnect> getListFTP(FtpConnect ftpConnect){
		boolean flag = Tool.generalConnects(gclient, Tool.dest_connect_popup);
		ArrayList<FtpConnect> ffile = new ArrayList<FtpConnect>();
		ArrayList<FtpConnect> ffolder = new ArrayList<FtpConnect>();
		ArrayList<FtpConnect> lstftp = new ArrayList<FtpConnect>();
		try{
			if(flag){
				System.out.println(" tool path -- " + Tool.dest_ftp_path_popup);
				System.out.println(" ftp path popup === " + Tool.getFTPPath(Tool.dest_ftp_path_popup)) ;
				listfileFTP= Tool.client.listFiles(Tool.getFTPPath(Tool.dest_ftp_path_popup));
				System.out.println(" list file " + listfileFTP.length);
				if(listfileFTP!=null && listfileFTP.length>0){

					for(FTPFile f : listfileFTP){
						FtpConnect tem = new FtpConnect();
						Drawable icon = null;
						if(f.isFile()){
							if (Tool.checkExtendFile(f.getName(),
							".txt")
							|| Tool.checkExtendFile(
									f.getName(), ".csv")) {
								icon = getResources()
								.getDrawable(
										R.drawable.text_file);
							} else if (Tool.checkExtendFile(
									f.getName(), ".xml")) {
								icon = getResources()
								.getDrawable(
										R.drawable.xml_file);
							} else if (Tool.checkExtendFile(
									f.getName(), ".flv")
									|| Tool.checkExtendFile(
											f.getName(), ".3gp")
											|| Tool.checkExtendFile(
													f.getName(), ".avi")) {										
								icon = getResources().getDrawable(R.drawable.video_file);
							} else if (Tool.checkExtendFile(
									f.getName(), ".mp3")) {
								icon = getResources()
								.getDrawable(
										R.drawable.mp3_file);
							} else if (Tool.checkExtendFile(
									f.getName(), ".doc")
									|| Tool.checkExtendFile(
											f.getName(), ".docx")) {
								icon = getResources()
								.getDrawable(
										R.drawable.word_file);
							} else if (Tool.checkExtendFile(
									f.getName(), ".ppt")
									|| Tool.checkExtendFile(
											f.getName(), ".pptx")) {
								icon = getResources()
								.getDrawable(
										R.drawable.pptx_file);
							} else if (Tool.checkExtendFile(
									f.getName(), ".xls")
									|| Tool.checkExtendFile(
											f.getName(), ".xlsx")) {
								icon = getResources()
								.getDrawable(
										R.drawable.xlsx_file);
							} else if (Tool.checkExtendFile(
									f.getName(), ".zip")
									|| Tool.checkExtendFile(
											f.getName(), ".rar")) {
								icon = getResources()
								.getDrawable(
										R.drawable.rar_file);
							} else if (Tool.checkExtendFile(
									f.getName(), ".jpg")
									|| Tool.checkExtendFile(
											f.getName(), ".jpeg")
											|| Tool.checkExtendFile(
													f.getName(), ".png")
													|| Tool.checkExtendFile(
															f.getName(), ".bmp")
															|| Tool.checkExtendFile(
																	f.getName(), ".gif")) {
								icon = getResources().getDrawable(R.drawable.image_file);
							} else if (Tool.checkExtendFile(
									f.getName(), ".apk")) {
								icon = getResources()
								.getDrawable(
										R.drawable.apk_file);
							} else if (Tool.checkExtendFile(
									f.getName(), ".exe")) {
								icon = getResources()
								.getDrawable(
										R.drawable.exe_file);
							} else {
								icon = getResources()
								.getDrawable(
										R.drawable.unknown_file);
							}

							tem.setHostname(ftpConnect.getHostname());
							tem.setPort(ftpConnect.getPort());
							tem.setUsername(ftpConnect.getUsername());
							tem.setPassword(ftpConnect.getPassword());
							tem.setIcon(icon);
							tem.setFtpFile(f);
							ffile.add(tem);
						}else if(f.isDirectory()){
							icon = getResources().getDrawable(R.drawable.folder_ftp);
							tem.setHostname(ftpConnect.getHostname());
							tem.setPort(ftpConnect.getPort());
							tem.setUsername(ftpConnect.getUsername());
							tem.setPassword(ftpConnect.getPassword());
							tem.setIcon(icon);
							tem.setFtpFile(f);

							ffolder.add(tem);
						}
					}

					lstftp.clear();
					if(ffolder!=null && ffolder.size()>0)
						lstftp.addAll(ffolder);

					if(ffile!=null && ffile.size()>0)
						lstftp.addAll(ffile);

				}else{
					lstftp = new ArrayList<FtpConnect>();
				}
			}
			else{
				System.out.println(" connect fail");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return lstftp;

	}
}
